package cn.zyl.demo.common.boot.constraint;

import cn.zyl.demo.common.boot.annotation.EncryptUserName;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * EncryptUserNameValidator类说明: 自定义校验负载类
 *
 * @author wsz
 * @version v1.0
 * @date 2020-08-10
 */
public class EncryptUserNameValidator implements ConstraintValidator<EncryptUserName,String> {
    private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z]\\w{5,20}$");
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 不为null才进行校验
        if (value != null) {
            Matcher matcher = PATTERN.matcher(value);
            return matcher.find();
        }
        return true;
    }
}
